//
//  ZhenYanDetailsViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/7/7.
//

#import "ZhenYanDetailsViewController.h"
#import "MottoModel.h"
#import "ZhenYanTopView.h"
#import "PingLunTableViewCell.h"
#import "PingLunTextView.h"
#import "WritingPopView.h"
@interface ZhenYanDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) ZhenYanTopView * topView;
@property (nonatomic , strong) MottoModel * mottoModel;

@end

@implementation ZhenYanDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"箴言详情");
    WS(weakSelf);
    [self zx_setRightBtnWithImg:SQGetImage(@"gengduo") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_reportButtonClick];
    }];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count <=0) {
        [self SQ_Header];
    }  
}
- (void)SQ_UIViewForSelfView{
    self.showEmpty = NO;
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    WS(weakSelf);
   
  
    [self.topView.pinglunBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_popPingLunView];
    }];
    [self SQ_tableViewAddRefresh];
   
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.userId forKey:@"id"];
    [HttpTool post:@"motto/details" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.mottoModel = [MottoModel mj_objectWithKeyValues:httpModel.data];
        self.topView.mottoModel = self.mottoModel;
        self.topView.frame = CGRectMake(0, 0, SCREEN_WIDETH, self.mottoModel.height+64+90);
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
 
  NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
  [dict setObject:@"1" forKey:@"current"];
  [dict setObject:@"20" forKey:@"size"];
  [dict setObject:self.userId forKey:@"pid"];
  [HttpTool post:@"motto/commentList" paramDic:dict successBlock:^(BaseModel * _Nonnull httpModel) {
      self.page = 1;
      [self.tableView.mj_header endRefreshing];
      NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
      NSArray * array = [MottoModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
      [self.jsonArray removeAllObjects];
      [self.jsonArray addObjectsFromArray:array];
      [self.tableView reloadData];
  } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
      [self.tableView.mj_header endRefreshing];
      [YJProgressHUD showMsgWithoutView:httpModel.message];
  }];
}
- (void)SQ_Foother{
  NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
  [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
  [dic setObject:@"20" forKey:@"size"];
  [dic setObject:self.userId forKey:@"pid"];
  [HttpTool post:@"motto/commentList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
      self.page ++;
      NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
      NSArray * array = [MottoModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
      [self.jsonArray addObjectsFromArray:array];
      [self.tableView reloadData];
      [self.tableView.mj_footer endRefreshing];
  } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
      [self.tableView.mj_footer endRefreshing];
  }];
}
-(void)SQ_reportButtonClick{
    WritingPopView * vc = [[WritingPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 260+FootHeight)];
    vc.uid = self.mottoModel.uid;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc.quxiaoBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    [vc.pingbiBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_mottoHide];
        [wk_pop dismiss];
    }];
   
}
-(void)SQ_mottoHide{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.userId forKey:@"rid"];
    [HttpTool post:@"motto/hide" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (self.hideWritingouchBlock) {
            self.hideWritingouchBlock();
        }
        [self SQ_popViewVC];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

-(void)SQ_popPingLunView{
    PingLunTextView *  view = [[PingLunTextView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 56)];
    LSTPopView * pop = [LSTPopView initWithCustomView:view
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.bgAlpha = 0;
    pop.isStack = YES;
    pop.avoidKeyboardSpace = 0;
    pop.dragStyle = LSTDragStyleY;
    pop.dragDistance = 40;
    [pop pop];
    LSTPopViewWK(pop)
    WS(weakSelf);
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    [view setTextViewBlock:^(NSString * _Nonnull string) {
        [wk_pop dismiss];
        [weakSelf SQ_setText:string];
    }];
}
-(void)SQ_setText:(NSString*)string{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:string forKey:@"content"];
    [dic setObject:self.userId forKey:@"pid"];
    [dic setObject:@"0" forKey:@"type"];
    [HttpTool post:@"motto/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count ;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PingLunTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PingLunTableViewCell"];
    if (!cell) {
        cell = [[PingLunTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"PingLunTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MottoModel * model = [self.jsonArray  objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [PingLunTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:20];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
 

- (ZhenYanTopView *)topView{
    if (!_topView) {
        _topView = [[ZhenYanTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 100)];
    }
    return _topView;
}

 
@end
